﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

namespace MouthAnimationFrame
{
    /// <summary>
    /// 与口型动画相关的拓展
    /// </summary>
    [LuaCallCSharp]
    public static class MouthAnimationExtension
    {
        #region C#端调用入口与出口
        /// <summary>
        /// 开始执行口型动画
        /// </summary>
        /// <param name="mouthAnimRef"></param>
        /// <param name="eventName"></param>
        [BlackList]
        public static void StartMouthAnimation(this Animator mouthAnimRef, string eventName)
        {
            try
            {
                InitMouthAnimation();

                if (mouthAnimRef == null)
                {
                    Debug.LogWarning("MouthAnimationSyncHelper.StartMouthAnimation() animator不能为空");
                    return;
                }
                MouthAnimation mAnim = MouthAnimationSyncHelper.GetInstance().GetAnimatorRef(mouthAnimRef);
                if (mAnim != null)
                {
                    if (mAnim.IsInit)
                    {
                        mAnim.StartPlayingMouthAnimationSyncAudio(eventName);
                    }
                    else
                    {
                        mAnim.InitMouthAnimation(mouthAnimRef);
                        mAnim.StartPlayingMouthAnimationSyncAudio(eventName);
                    }
                }
                else
                {
                    MouthAnimation newAnim = mouthAnimRef.gameObject.GetComponent<MouthAnimation>();
                    if (newAnim == null)
                    {
                        newAnim = mouthAnimRef.gameObject.AddComponent<MouthAnimation>();
                    }

                    newAnim.InitMouthAnimation(mouthAnimRef);
                    newAnim.StartPlayingMouthAnimationSyncAudio(eventName);
                }
            }
            catch (Exception e)
            {
                Debug.LogError("StartMouthAnimation方法报错: " + e);
            }
        }

        /// <summary>
        /// 停止对话动画
        /// </summary>
        /// <param name="mouthAnimRef"></param>
        [BlackList]
        public static void StopMouthAnimation(this Animator mouthAnimRef)
        {
            try
            {
                MouthAnimation mouthAnim = MouthAnimationSyncHelper.GetInstance().GetAnimatorRef(mouthAnimRef);
                if(mouthAnim != null)
                {
                    mouthAnim.StopMouthAnimation();
                }
            }
            catch (Exception e)
            {
                Debug.LogError("StopMouthAnimation方法报错: " + e);
            }
        }
        #endregion

        #region Lua初始化
        /// <summary>
        /// 初始化延迟加载
        /// </summary>
        [BlackList]
        public static void InitMouthAnimation()
        {
            if (!MouthAnimationSyncHelper.GetInstance().IsInitialized())
            {
                MouthAnimationSyncHelper.GetInstance().Initialize();
            }
        }

        #endregion

        #region Lua端入口与出口
        /// <summary>
        /// Lua端开始执行对话动画
        /// </summary>
        /// <param name="animator"></param>
        /// <param name="eventName"></param>
        public static void StartMouthAnimation_Lua(Animator animator, string eventName)
        {
            InitMouthAnimation();
            animator?.StartMouthAnimation(eventName);
        }
        /// <summary>
        /// Lua端停止执行对话动画
        /// </summary>
        /// <param name="animator"></param>
        /// <param name="eventName"></param>
        public static void StopMouthAnimation_Lua(Animator animator)
        {
            animator?.StopMouthAnimation();
        }
        #endregion
    }
}